//
//  StatusOriginalView.swift
//  个人微博
//
//  Created by guo3920591 on 16/4/21.
//  Copyright © 2016年 guo3920591. All rights reserved.
//

import UIKit

class StatusOriginalView: UIView {
    
    /// 微博视图模型
    var viewModel: StatusViewModel?{
        
        didSet{
            //设置数据
            //设置图像
            iconView.xl_setImageUrl(viewModel?.status.user?.profile_image_url, size: CGSizeMake(36, 36), isCorner: true, placeHoder: "avatar_default_big")
            //设置姓名
            nameLabel.text = viewModel?.status.user?.screen_name
            //设置正文
            contentLabel.text = viewModel?.status.text
            //设置时间
            timeLabel.text = NSDate.xl_sinaDate(viewModel?.status.created_at)?.xl_descriptiondate
            //设置来源
            sourceLabel.text = viewModel?.sourceText
            //设置会员等级
            memberIconView.image = viewModel?.memberIconImage
            //用户认证
            vipIconView.image = viewModel?.vipIcionImage
            
            //’更新‘配图视图大小
            
            let offset = viewModel?.status.pic_urls?.count > 0 ? StatusCellLayout.margin : 0
            pictureView.snp_updateConstraints { (make) -> Void in
            
                make.top.equalTo(contentLabel.snp_bottom).offset(offset)
               
                make.size.equalTo(viewModel!.pictureViewSize)
            
            }
            
            //设置配图内容
            
            pictureView.pic_urls = viewModel?.status.pic_urls
            
            //设置配图视图大小
            
           pictureView.updataImageSize(viewModel!.pictureViewSize)
            
            
        }
    }
    

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private let iconView:UIImageView = UIImageView(imageName: "avatar_default_big")
    
    private let  nameLabel:UILabel = UILabel(text: "传智博客")
    
    private let memberIconView:UIImageView = UIImageView(imageName: "common_icon_membership_level1")
    
    private let vipIconView:UIImageView = UIImageView(imageName: "avatar_vip")
    
    private let timeLabel:UILabel = UILabel(text: "创建时间", textColor: GXLapplearceColor, fontsize: 10)
    
    private let sourceLabel:UILabel = UILabel(text: "来源",  fontsize: 10 )
    
    private let contentLabel:UILabel = UILabel(text: "微博微博", fontsize: 15)
    /// 配图视图
    private let pictureView: StatusPictureView = StatusPictureView()
    
    
    
    
    
    
    
}
// MARK: - 设置界面
extension StatusOriginalView{
    
    private func setupUI(){
        
        backgroundColor = UIColor.whiteColor()
        
        //添加控件
        
        addSubview(iconView)
        addSubview(nameLabel)
        addSubview(memberIconView)
        addSubview(vipIconView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(contentLabel)
        addSubview(pictureView)
        
        let margin: CGFloat = 12.0
        let imageSize = CGSize(width: 36, height: 36)
    
        
        //测试头像
        UIImage(named: "avatar_default_big")?.cz_asyncDrawImage(imageSize, isCorner: true,  finished: { (image) -> () in
            
            self.iconView.image = image 
        })
        
        //自动布局
        
        iconView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(margin)
            make.leading.equalTo(margin)
            make.size.equalTo(imageSize)
        }

        nameLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconView)
            make.leading.equalTo(iconView.snp_trailing).offset(margin)
            
            }

        memberIconView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(nameLabel)
            make.left.equalTo(nameLabel.snp_right).offset(margin)
        }

        vipIconView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView.snp_right).offset(-5)
            make.centerY.equalTo(iconView.snp_bottom).offset(-5)
        }

        timeLabel.snp_makeConstraints { (make) -> Void in
            
            make.bottom.equalTo(iconView)
            make.left.equalTo(nameLabel)
        }

        sourceLabel.snp_makeConstraints { (make) -> Void in
            
            make.bottom.equalTo(timeLabel)
            make.left.equalTo(timeLabel.snp_right).offset(margin)
        }
        
        contentLabel.snp_makeConstraints { (make) -> Void in
            
            make.left.equalTo(iconView)
            
            make.top.equalTo(iconView.snp_bottom).offset(margin)
            
            make.right.equalTo(self).offset(-margin)
            
        }
        
        
        pictureView.snp_makeConstraints { (make) -> Void in
            
            
            make.top.equalTo(contentLabel.snp_bottom).offset(margin)
            make.left.equalTo(contentLabel)
            
            
            make.size.equalTo(CGSizeZero)
            //设置底部约束很重要
            make.bottom.equalTo(self).offset(-margin)
            
        }
        
        
    }
    
}
